package com.bcxj.servicedriveruser.servcie;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bcxj.internalcommon.constant.CommonStatusEnum;
import com.bcxj.internalcommon.constant.DriverCarConstants;
import com.bcxj.internalcommon.dto.ResponseResult;
import com.bcxj.internalcommon.entity.Car;
import com.bcxj.internalcommon.entity.DriverCarBindingRelationship;
import com.bcxj.internalcommon.entity.DriverUser;
import com.bcxj.internalcommon.entity.DriverUserWorkStatus;
import com.bcxj.internalcommon.response.OrderDriverResponse;
import com.bcxj.servicedriveruser.mapper.CarMapper;
import com.bcxj.servicedriveruser.mapper.DriverCarBindingRelationshipMapper;
import com.bcxj.servicedriveruser.mapper.DriverUserMapper;
import com.bcxj.servicedriveruser.mapper.DriverUserWorkStatusMapper;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import javax.xml.ws.Response;
import java.time.LocalDateTime;

/**
 * ClassName: UserService
 * Package: com.bcxj.servicedriveruser.servcie
 * Description:
 *
 * @Author BCXJ
 * @Create 2024/10/19 11:22
 * @Version 1.0
 * @Since 1.0
 */
@Service
public class DriverUserService {

    @Autowired
    private DriverUserMapper driverUserMapper;

    @Autowired
    private DriverUserWorkStatusMapper driverUserWorkStatusMapper;

    @Autowired
    private DriverCarBindingRelationshipMapper driverCarBindingRelationshipMapper;
    @Autowired
    private CarMapper carMapper;

    /**
     * 新增司机
     *
     * @param driverUser
     * @return
     */
    public ResponseResult addUser(DriverUser driverUser) {
        // 检查司机的手机号是否已经存在
        Integer count = driverUserMapper.selectCount(new LambdaQueryWrapper<DriverUser>().eq(DriverUser::getDriverPhone, driverUser.getDriverPhone()));
        if (count > 0) {
            return ResponseResult.fail(CommonStatusEnum.DRIVER_PHONE_EXIST);
        }

        // 添加司机的信息
        LocalDateTime now = LocalDateTime.now();
        // 补充信息
        driverUser.setGmtCreate(now);
        driverUser.setGmtModified(now);
        driverUserMapper.insert(driverUser);

        // 初始化司机的状态为收车并且插入数据
        DriverUserWorkStatus driverUserWorkStatus = new DriverUserWorkStatus();
        driverUserWorkStatus.setDriverId(driverUser.getId());
        driverUserWorkStatus.setWorkStatus(DriverCarConstants.DRIVER_WORK_STATUS_STOP);
        driverUserWorkStatus.setGmtCreate(now);
        driverUserWorkStatus.setGmtModified(now);
        driverUserWorkStatusMapper.insert(driverUserWorkStatus);

        return ResponseResult.success();
    }


    /**
     * 更新司机的信息
     *
     * @param driverUser
     * @return
     */
    public ResponseResult updateUser(DriverUser driverUser) {
        LocalDateTime now = LocalDateTime.now();

        // 补充信息
        driverUser.setGmtModified(now);
        driverUser.setGmtCreate(now);

        driverUserMapper.updateById(driverUser);
        return ResponseResult.success();
    }


    public ResponseResult<DriverUser> getDriverUserByPhone(String driverPhone) {
        // 根据手机号查询司机信息
        LambdaQueryWrapper<DriverUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DriverUser::getDriverPhone, driverPhone);
        queryWrapper.eq(DriverUser::getState, DriverCarConstants.DRIVER_STATE_VALID);

        // 判断信息
        // 如果不存在司机信息，则返回错误，司机不存在
        DriverUser driverUser = driverUserMapper.selectOne(queryWrapper);
        if (null == driverUser) {
            return ResponseResult.fail(CommonStatusEnum.DRIVER_NOT_EXIST);
        }

        return ResponseResult.success(driverUser);
    }


    /**
     * 根据carId 找到对应的状态为出车状态的司机的信息
     *
     * @param carId
     * @return
     */
    public ResponseResult<OrderDriverResponse> getAvailableDriver(String carId) {
        // 1 查找车辆与司机的绑定的信息
        DriverCarBindingRelationship driverCarBindingRelationship = driverCarBindingRelationshipMapper.selectOne(new LambdaQueryWrapper<DriverCarBindingRelationship>()
                .eq(DriverCarBindingRelationship::getCarId, carId)
                .eq(DriverCarBindingRelationship::getBindState, DriverCarConstants.DRIVER_CAR_BIND));

        // 如果车辆暂时没有绑定司机, 一般不会走这个逻辑的
        if (null == driverCarBindingRelationship) {
            return ResponseResult.fail(CommonStatusEnum.DRIVER_NOT_EXIST);
        }

        // 获取司机的id
        Long driverId = driverCarBindingRelationship.getDriverId();

        // 2 查找司机的状态是否为出车
        Integer count = driverUserWorkStatusMapper.selectCount(new LambdaQueryWrapper<DriverUserWorkStatus>()
                .eq(DriverUserWorkStatus::getDriverId, driverId)
                .eq(DriverUserWorkStatus::getWorkStatus, DriverCarConstants.DRIVER_WORK_STATUS_START));

        if (0 == count) {
            return ResponseResult.fail(CommonStatusEnum.AVAILABLE_DRIVER_EMPTY);
        } else {
            // 查询司机的信息
            DriverUser driverUser = driverUserMapper.selectOne(new LambdaQueryWrapper<DriverUser>()
                    .eq(DriverUser::getId, driverId)
                    .eq(DriverUser::getState, DriverCarConstants.DRIVER_STATE_VALID));

            // 查询车辆的信息
            Car car = carMapper.selectById(carId);

            // 封装信息
            OrderDriverResponse orderDriverResponse = new OrderDriverResponse();
            orderDriverResponse.setCarId(Long.parseLong(carId));
            orderDriverResponse.setDriverId(driverUser.getId());
            orderDriverResponse.setDriverPhone(driverUser.getDriverPhone());
            orderDriverResponse.setLicenseId(driverUser.getLicenseId());
            orderDriverResponse.setVehicleNo(car.getVehicleNo());
            orderDriverResponse.setVehicleType(car.getVehicleType());
            return ResponseResult.success(orderDriverResponse);
        }
    }


    /**
     * 根据司机手机号获取car信息
     *
     * @param driverPhone
     * @return
     */
    public ResponseResult<DriverCarBindingRelationship> getDriverCarBindingRelationship(String driverPhone) {
        // 查询司机的id
        DriverUser driverUser = driverUserMapper.selectOne(new LambdaQueryWrapper<DriverUser>()
                .eq(DriverUser::getDriverPhone, driverPhone));

        DriverCarBindingRelationship driverCarBindingRelationship = driverCarBindingRelationshipMapper.selectOne(new LambdaQueryWrapper<DriverCarBindingRelationship>()
                .eq(DriverCarBindingRelationship::getDriverId, driverUser.getId())
                .eq(DriverCarBindingRelationship::getBindState, DriverCarConstants.DRIVER_CAR_BIND));
        return ResponseResult.success(driverCarBindingRelationship);
    }
}
